package com.kc.web.controller.business;

import com.kc.business.domain.BizGoodsSkuCategory;
import com.kc.business.enums.GoodsSkuCategoryTypeEnum;
import com.kc.business.service.IBizGoodsSkuCategoryService;
import com.kc.common.annotation.Log;
import com.kc.common.annotation.RepeatSubmit;
import com.kc.common.core.controller.BaseController;
import com.kc.common.core.domain.AjaxResult;
import com.kc.common.core.page.TableDataInfo;
import com.kc.common.enums.BusinessType;
import io.swagger.annotations.Example;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 商品sku类别controller
 */
@RestController
@RequestMapping("/business/skuCategory")
public class BizGoodsSkuCategoryController extends BaseController {
    @Resource
    private IBizGoodsSkuCategoryService skuCategoryService;

    /**
     * sku类别列表
     * @param bizGoodsSkuCategory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:skuCategory:getList')")
    @GetMapping
    public TableDataInfo getList(BizGoodsSkuCategory bizGoodsSkuCategory) {
        if(bizGoodsSkuCategory == null){
            bizGoodsSkuCategory.setTenantType(GoodsSkuCategoryTypeEnum.PLATFORM.getValue());
        }
        startPage();
        bizGoodsSkuCategory.setIsDeleted(0);
        return getDataTable(skuCategoryService.getList(bizGoodsSkuCategory));
    }
    /**
     * 类别新增
     * @param bizGoodsSkuCategory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:skuCategory:add')")
    @Log(title = "商品sku类别管理", businessType = BusinessType.INSERT)
    @PostMapping
    @RepeatSubmit
    public AjaxResult add(@Validated @RequestBody BizGoodsSkuCategory bizGoodsSkuCategory) {
        bizGoodsSkuCategory.setIsDeleted(0);
        if(bizGoodsSkuCategory == null){
            bizGoodsSkuCategory.setTenantType(GoodsSkuCategoryTypeEnum.PLATFORM.getValue());
        }
        BizGoodsSkuCategory goodsSkuCategory = new BizGoodsSkuCategory();
        goodsSkuCategory.setName(bizGoodsSkuCategory.getName());
        goodsSkuCategory.setIsDeleted(0);
        goodsSkuCategory.setTenantType(bizGoodsSkuCategory.getTenantType());
        if(null != skuCategoryService.getInfo(goodsSkuCategory)){
            return AjaxResult.error("类别名称已存在");
        }
        return toAjax(skuCategoryService.add(bizGoodsSkuCategory));
    }

    /**
     * 类别修改
     * @param bizGoodsSkuCategory
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:skuCategory:edit')")
    @Log(title = "商品sku类别管理", businessType = BusinessType.UPDATE)
    @PutMapping
    @RepeatSubmit
    public AjaxResult edit(@Validated @RequestBody BizGoodsSkuCategory bizGoodsSkuCategory) {
        BizGoodsSkuCategory goodsSkuCategory = new BizGoodsSkuCategory();
        goodsSkuCategory.setName(bizGoodsSkuCategory.getName());
        goodsSkuCategory.setIsDeleted(0);
        goodsSkuCategory.setTenantType(bizGoodsSkuCategory.getTenantType());
        goodsSkuCategory = skuCategoryService.getInfo(goodsSkuCategory);
        if(null != goodsSkuCategory.getId() && !bizGoodsSkuCategory.getId().equals(goodsSkuCategory.getId())){
            return AjaxResult.error("类别名称已存在");
        }

        if(bizGoodsSkuCategory.getIsDeleted() == null) {
            bizGoodsSkuCategory.setIsDeleted(0);
        }
        if(bizGoodsSkuCategory == null){
            bizGoodsSkuCategory.setTenantType(GoodsSkuCategoryTypeEnum.PLATFORM.getValue());
        }
        return toAjax(skuCategoryService.edit(bizGoodsSkuCategory));
    }

    /**
     * 类别删除
     * @param id
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:skuCategory:remove')")
    @Log(title = "商品sku类别管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    @RepeatSubmit
    public AjaxResult remove(@PathVariable("id") Integer id) {
        BizGoodsSkuCategory bizGoodsSkuCategory = new BizGoodsSkuCategory();
        bizGoodsSkuCategory.setId(id);
        bizGoodsSkuCategory = skuCategoryService.getInfo(bizGoodsSkuCategory);
        bizGoodsSkuCategory.setIsDeleted(1);
        return toAjax(skuCategoryService.remove(bizGoodsSkuCategory));
    }

    /**
     * 根据商品id获取配置信息
     * @param goodsId
     * @return
     */
    @PreAuthorize("@ss.hasPermi('business:skuCategory:getListByGoodsId')")
    @GetMapping("/getListByGoodsId/{goodsId}")
    public TableDataInfo getListByGoodsId(@PathVariable("goodsId") Integer goodsId){
        return getDataTable(skuCategoryService.getListByGoodsId(goodsId));
    }
}
